<?php if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){?>
<div class="col-md-8">
<div class="main-content" id="main-content">
	<div class="sub-content">
		<div class="top-header page-header clearfix">
			<h1>Buy Processing</h1>
		</div><br/>
		<div class="user-profile1 col-md-12">
			<form id="buyform" name="buyform"  method="post" class="form-horizontal" role="form">
			
				<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin"){?>
					<div class="form-group">
								<label class="control-label col-sm-3">Admin</label>
							<div class="col-sm-9">
							<div class="select_6">
								<select id="admindropdown" name="admindropdown" onchange="getUsers('users')" class="form-control">
									<option value="">Select The Admin</option>
									<? if(count($getalladmins)>0){foreach($getalladmins as $admins){
									if($admins->user_id==$this->getproductDetails->parent_user_id){?>
										<option value="<? echo $admins->user_id;?>"><? echo $admins->company_name;?></option>
									<?}else{?>
										<option value="<? echo $admins->user_id;?>"><? echo $admins->company_name;?></option>
									<?}
									?>
									<?}}?>
								</select>
								</div>
							</div>
					</div><span style="color:red" id="erroradmin"></span>	
				<? }if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']!="user"){?>					
					<div class="form-group">
									<label class="control-label col-sm-3">User</label>
									<div class="col-sm-9">
									<div class="select_6">
										<select id="userdropdown" name="userdropdown" class="form-control">
											<option value="">Select a user</option>
										</select>
										</div>
									</div>	
					</div><span style="color:red" id="erroruser"></span>
				<?} else if($_SESSION['user']['user_type']=="user"){?>
					<div class="form-group" id="hiddentbox">
						<label class="control-label col-sm-3">User Name</label>
						<div class="col-sm-9">
						<input type="text" name="usertextbox" id="usertextbox" class="form-control" readonly="readonly" value="<? if(isset($_SESSION['user']['username'])){ echo $_SESSION['user']['username'];} ?>">
						<input type="hidden" name="userdropdown" id="userdropdown" value="<? if(isset($_SESSION['user']['user_id'])){ echo $_SESSION['user']['user_id'].'/'.$_SESSION['user']['username'];} ?>"></div>
					</div>
					<?}?>
					<div class="form-group">
						<label class="control-label col-sm-3">Product Name</label>
						<div class="col-sm-9"><input type="hidden"  id="pname" name="pname" value="<? echo $this->getproductDetails->genericname;?>"/>
						<input type="text" class="input_type_text form-control" readonly="readyonly" value="<? echo $this->getproductDetails->genericname;?>"/></div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Quantity</label>
						<div class="col-sm-9"><input type="hidden" id="quntity" name="quantity" value="<? echo $this->getproductDetails->quntity;?>"/>
						<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->quntity.' '.$this->getproductDetails->unites;?>"/></div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Actual Price</label>
							<div class="col-sm-9">
							<? if(isset($this->getproductDetails->suggested_price) && $this->getproductDetails->suggested_price!=""){?>
								<input type="hidden" id="acprice" name="acprice" value="<? echo $this->getproductDetails->suggested_price;?>" />
								<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->suggested_price;?>" />						
							<?}?>	</div>		
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Available Packs</label>
							<div class="col-sm-9">
							<? if(isset($this->getproductDetails->per) && $this->getproductDetails->per!=""){?>
								<input type="hidden" id="hidBuyproductPacks" name="hidBuyproductPacks" value="<? echo $this->getproductDetails->package;?>" />
								<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->package;?>" />						
							<?}?></div>			
					</div>
					<div class="form-group">
						<label class="control-label col-sm-3">Per Pack</label>
							<div class="col-sm-9">
							<? if(isset($this->getproductDetails->per) && $this->getproductDetails->per!=""){?>
								<input type="hidden" id="hidBuyproductPer" name="hidBuyproductPer" value="<? echo $this->getproductDetails->per;?>" />
								<input type="text" class="input_type_text form-control" readonly="readonly" value="<? echo $this->getproductDetails->per.' '.$this->getproductDetails->unites;?>" />						
							<?}?></div>			
					</div>
					<? if(isset($this->getproductDetails->saleing) && $this->getproductDetails->saleing!="Wholesale"){?>
					<div class="form-group">
						<label class="control-label col-sm-3">Enter Packs</label>
							
						<div class="col-sm-9"><input type="text" class="input_type_text form-control" id="userPacks" name="userPacks" value="" /></div>					
									
					</div>
					<div style="color:red" id="erroruserPacks"></div>
					<?}?>	
					<div class="form-group">
					
							<div class="col-sm-3">&nbsp;</div>
						<div class="col-sm-9">
							   <a href="javascript:void(0)" class="btn btn-danger" role="button" onclick="return buyFun()" value="submit">Buy</a>
							   <a class="back_btn btn btn-danger" role="button" href="<? echo $this->Url('products')?>/sales">Back</a>
							   <div id="loading" style="display:none;float:right;margin-right:30px;"><img src="<?php echo $this->basePath() ?>/images/loading_image.gif" /></div>
							
						</div>
					
					<input type="hidden" id="hidBuyproductType" name="hidBuyproductType" value="<? if(isset($this->getproductDetails->saleing)){echo $this->getproductDetails->saleing;}?>">
					<input type="hidden" id="hidBuyproduct" name="hidBuyproduct" value="<? if(isset($_GET['product'])){echo $_GET['product'];}?>">
					<input type="hidden" value="<? if(isset($this->getproductDetails->user_id) && $this->getproductDetails->user_id!=""){ echo $this->getproductDetails->user_id;}?>" id="hidajx_user_id" name="hidajx_user_id">
					<input type="hidden" id="check_product" name="check_product" value="0">
				
		   	</form>
		</div>
	</div>
</div></div>
<script type="text/javascript">
$(function(){
	var productid=$("#hidBuyproduct").val();	
	$.ajax({
		type: "POST",
		url:   	BASE_PATH+'/products/checking-product',
		data: {productid:productid},
			success: function(data){
				if(data.output=='notsuccess'){
					$("#check_product").val('1');
				}else if(data.output=='success'){
					$("#check_product").val('0');
				}
			}
		});
})
$(function(){
var type1='user';
	getUsers(type1);
});
	function getUsers(type){ 
		var hid_user_id='';
		<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']!="admin"){?>
			<?php if(isset($_SESSION['products']['admin_user_id']) && $_SESSION['products']['admin_user_id']!=""){?>
				var user_id=$('#admindropdown').val();			
			<?php }else{?>
				var user_id=$('#admindropdown').val();	
			<?php }?>
		<?}else{?>
			var user_id=<? if(isset($_SESSION['user']['user_id'])){ echo $_SESSION['user']['user_id'];}?>;	
		<?}?>
		if(type==''){
			if($("#hidajx_user_id").val()!=""){
				var hid_user_id=$("#hidajx_user_id").val();
			}
		}else{
			var hid_user_id=$("#hidajx_user_id").val();
		}
		var check='buycheck';
		if(user_id!=''){
			$.ajax
			({
			type: "GET",
			url:   	BASE_PATH+'/products/product-ajax?user_id='+ user_id +'&user_user_id='+ hid_user_id +'&check='+check,
			success: function(data)
			{
				if(data.getadminUsers==''){
				<? if(isset($_SESSION['users']['user_type']) && $_SESSION['users']['user_type']!="user") {?>
					alert('There are no users under selected admin'); 
				<? } ?>
					$("#userdropdown").html('<option value="">Select User</option>');
				}else{
					$("#userdropdown").html(data.getadminUsers );
				}
			
			}
			});
		}
	
	}
function buyFun(){  
	$("#loading").show();
	var flag=true;
	var admindrop=$("#admindropdown").val();
	var userdrop=$("#userdropdown").val();
	var check_product=$("#check_product").val();
	if(check_product==1){
		$("#loading").hide();
		alert('Product not available');return false;		
	}
	if(admindrop=="")
		{
			$('#erroradmin').html('*Select the admin');	
				flag=false;
		}else{
			$('#erroradmin').html('');
		}
		if(userdrop=='')
		{
			$('#erroruser').html('*Select the user');	
				flag=false;
		}else{
			$('#erroruser').html('');
		}
		if($('#hidBuyproductType').val()=='Partly'){
			var value=$('#userPacks').val();
			if($('#userPacks').val()=='' || $('#userPacks').val()==0){
					$('#erroruserPacks').html('*Enter Packs Less than or equel to'+$('#hidBuyproductPacks').val());	
					flag=false;	
			}else{
				if (!isNaN(value) && value.toString().indexOf('.') != -1){
					$('#erroruserPacks').html('*Please enter only integer values');	
					flag=false;	
				}else{
					var totalPacks=$('#hidBuyproductPacks').val();
					var userPacks=$('#userPacks').val();
					if(parseInt(totalPacks) >= parseInt(userPacks)){
						$('#erroruserPacks').html('');	
					}else{
						$('#erroruserPacks').html('*Enter Packs Less than or equel to'+$('#hidBuyproductPacks').val());	
						flag=false;	
					}
				}
			}
		}		
		if(flag==false){
			$("#loading").hide();
			return false;
		}else{
			$("#buyform").submit();
		}
}
$(function(){
	<? if(isset($_GET['success']) && $_GET['success']=='success'){?>
	$("#loading").hide();
	alert("Your request has been successfully. \n Wait for approvaling.");
	 window.location=BASE_URL+"/products/sales";
	<?}?>
	
});

</script>
<?php }else{?>
<script>
	window.location='<? echo $this->Url('home')?>';
</script>
<?php }?>








